//
//  NosenegHomeViewController.m
//  NordsonTesco
//
//  Created by mac on 16/6/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "NosenegHomeViewController.h"
#import "HomeCarouselFigure.h"
#import "HeaderCell.h"
#import "HomeSearchViewController.h"
#import "TLCityPickerController.h"

@interface NosenegHomeViewController ()<UISearchBarDelegate,SDCycleScrollViewDelegate>
{
    UISearchBar *homeSearch;//搜索
    UIView *navigationHomeView;//导航背景
    UIButton *messageButton;//消息
    UIButton *sweepButton;//扫一扫
    HomeCarouselFigure *homeCarouse;
    HeaderCell *headerCell;
    UIButton *localitiesButon;

}
@end

@implementation NosenegHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNavigation];//设置navigation
    //    [self homeSearch];//搜索
    [self collectionViewInitAction];//collectionV
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    navigationHomeView.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    //    self.navigationController.navigationBarHidden = YES;
}


#pragma mark -- collectionView
- (void)collectionViewInitAction{
    self.homeCollectionView .delegate = self;
    self.homeCollectionView .dataSource = self;
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier : @"header" forIndexPath :indexPath];
    
    return cell;
}

//跳转
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    NSLog(@"indexPath.row:  %ld",(long)indexPath.row);
////    CommodityModel *model = [guessArray objectAtIndex:indexPath.row];
////    [self performSegueWithIdentifier:@"commodityDetailInfo" sender:model];
//}

//headercell
- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *reusableView = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        headerCell = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        //第一块 轮播图
        //        homeCarouse = [[HomeCarouselFigure alloc]initWithFrame:CGRectMake(0, 0, kScrernWidth, 200)];
        //        [self.view addSubview:homeCarouse];
        NSArray *images = @[[UIImage imageNamed:@"1"],
                            [UIImage imageNamed:@"2"],
                            [UIImage imageNamed:@"3"],
                            [UIImage imageNamed:@"4"]
                            ];
        SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,0,kScrernWidth,240) imagesGroup:images];
        
        cycleScrollView.infiniteLoop = YES;
        cycleScrollView.delegate = self;
        cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        [self.view addSubview:cycleScrollView];
        //         --- 轮播时间间隔，默认1.0秒，可自定义
        //cycleScrollView.autoScrollTimeInterval = 4.0;
        
        reusableView = headerCell;
    }
    return reusableView;
}
#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    NSLog(@"---点击了第%ld张图片", (long)index);
}
#pragma  mark -- 设置navigation
- (void)setUpNavigation{
    
    navigationHomeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,kScrernWidth, 64)];
    navigationHomeView.backgroundColor = [UIColor colorWithRed:0.99 green:0.32 blue:0.12 alpha:1.00];
    [self.navigationController.view addSubview:navigationHomeView];
    
    UIImageView *searchImage = [[UIImageView alloc]initWithFrame:CGRectMake(navigationHomeView.center.x - 80,35, 15, 15)];
    searchImage.image = [UIImage imageNamed:@"fd"];
    [navigationHomeView addSubview:searchImage];
    
    UILabel *searchLabel = [[UILabel alloc]initWithFrame:CGRectMake(navigationHomeView.center.x - 50,32, 60, 20)];
    searchLabel.text = @"请输入商家名称";
    searchLabel.textColor = [UIColor whiteColor];
    searchLabel.font = [UIFont systemFontOfSize:12];
    [navigationHomeView addSubview:searchLabel];
    
    //搜索透明view
    UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(navigationHomeView.center.x - 90,30,180,25)];
    searchView.backgroundColor = [UIColor whiteColor];
    searchView.alpha = 0.3;
    searchView.layer.masksToBounds = YES;
    searchView.layer.cornerRadius = 15;
    [navigationHomeView addSubview:searchView];
    
    UIButton *searchButon = [UIButton buttonWithType:UIButtonTypeCustom];
    searchButon.frame = CGRectMake(navigationHomeView.center.x - 90,30,180,25);
    [searchButon addTarget:self action:@selector(searchButonAction) forControlEvents:UIControlEventTouchUpInside];
    searchButon.backgroundColor = [UIColor clearColor];
    searchButon.layer.masksToBounds = YES;
    searchButon.layer.cornerRadius = 15;
    [navigationHomeView addSubview:searchButon];
    
    //地区
    localitiesButon = [UIButton buttonWithType:UIButtonTypeCustom];
    localitiesButon.frame = CGRectMake(10,30,70,25);
    [localitiesButon addTarget:self action:@selector(localitiesButonAction) forControlEvents:UIControlEventTouchUpInside];
    [localitiesButon setTitle:@"青岛" forState:UIControlStateNormal];
    //    localitiesButon.backgroundColor = [UIColor greenColor];
    localitiesButon.titleLabel.font = [UIFont systemFontOfSize:15];
    [navigationHomeView addSubview:localitiesButon];
    
    //左(扫一扫)
    sweepButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [sweepButton addTarget:self action:@selector(sweepButtonAction) forControlEvents:UIControlEventTouchUpInside];
    CGSize sweepButtonSize = CGSizeMake(25, 25);
    sweepButton.frame = CGRectMake(navigationHomeView.bounds.size.width - 65,27, sweepButtonSize.width, sweepButtonSize.height);
    //    [sweepButton setImage:[UIImage imageNamed:@"sao"] forState:UIControlStateNormal];
//    sweepButton.backgroundColor = [UIColor greenColor];
    [navigationHomeView addSubview:sweepButton];
    
    //右(消息)
    messageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [messageButton addTarget:self action:@selector(messageButtonAction) forControlEvents:UIControlEventTouchUpInside];
    CGSize messageButtonSize = CGSizeMake(25, 25);
    messageButton.frame = CGRectMake(navigationHomeView.bounds.size.width - 35,27, messageButtonSize.width, messageButtonSize.height);
        [messageButton setImage:[UIImage imageNamed:@"xiao"] forState:UIControlStateNormal];
    [navigationHomeView addSubview:messageButton];
}
- (void)localitiesButonAction{
//    NSLog(@"青岛");
    TLCityPickerController *cityPickerVC = [[TLCityPickerController alloc] init];
    [cityPickerVC setDelegate:self];
    
    cityPickerVC.locationCityID = @"1400010000";
    //    cityPickerVC.commonCitys = [[NSMutableArray alloc] initWithArray: @[@"1400010000", @"100010000"]];        // 最近访问城市，如果不设置，将自动管理
    cityPickerVC.hotCitys = @[@"100010000", @"200010000", @"300210000", @"600010000", @"300110000"];
    
    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:cityPickerVC] animated:YES completion:^{
        
    }];
}

- (void)searchButonAction{
    NSLog(@"搜索");
    HomeSearchViewController *homeSeach = [[HomeSearchViewController alloc]init];
    navigationHomeView.hidden = YES;
    [self.navigationController pushViewController:homeSeach animated:YES];
}
//#pragma mark -- 搜索
//- (void)homeSearch{
//    homeSearch = [[UISearchBar alloc] initWithFrame:CGRectMake(70, 26, kScrernWidth - 150, 31)];
//    homeSearch.placeholder = @"输入关键词";
//    [homeSearch resignFirstResponder];
//    homeSearch.layer.cornerRadius = 10;
//    homeSearch.clipsToBounds = YES;
//    homeSearch.delegate = self;
//    //     [homeSearch sizeToFit];
//    [self setSearchTextFieldBackgroundColor:[UIColor whiteColor]];
//    [navigationHomeView addSubview:homeSearch];
//}
////去灰底的方法
//- (void)setSearchTextFieldBackgroundColor:(UIColor *)backgroundColor
//{
//    UIView *searchTextField = nil;
//    homeSearch.barTintColor =[UIColor colorWithRed:246/255.0 green:51/255.0 blue:11/255.0 alpha:1];
//    searchTextField.backgroundColor = backgroundColor;
//}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"搜索");
}
- (void)sweepButtonAction{
    NSLog(@"扫一扫");
}
- (void)messageButtonAction{
    NSLog(@"消息");
}

//第二块
- (IBAction)commodityModule:(UIButton *)sender {
    switch (sender.tag) {
        case 100:{
            NSLog(@"1");
        }
            break;
        case 101:{
            NSLog(@"2");
        }
            break;
        case 102:{
            NSLog(@"3");
        }
            break;
        case 103:{
            NSLog(@"4");
        }
            break;
        case 104:{
            NSLog(@"5");
        }
            break;
        case 105:{
            NSLog(@"6");
        }
            break;
        case 106:{
            NSLog(@"7");
        }
            break;
        case 107:{
            NSLog(@"8");
        }
            break;
            
        default:
            break;
    }
}


#pragma mark - TLCityPickerDelegate
- (void) cityPickerController:(TLCityPickerController *)cityPickerViewController didSelectCity:(TLCity *)city
{
    [localitiesButon setTitle:city.cityName forState:UIControlStateNormal];
    [cityPickerViewController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void) cityPickerControllerDidCancel:(TLCityPickerController *)cityPickerViewController
{
    [cityPickerViewController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
